package com.hxkcloud.patient.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.MonitorKindTestBean;
import com.hxkcloud.patient.utils.BrightnessUtils;
import com.hxkcloud.patient.wheelview.WheelView;
import com.hxkcloud.patient.wheelview.adapter.ArrayWheelAdapter;

import java.util.List;

/**
 * Created by william_sun on 2016-08-04.
 * Description:患者监测项录入，餐前餐后选择
 */
public class MonitorInputPopWindow extends PopupWindow implements View.OnClickListener {

    private Activity context;
    private WheelView wheel_addr;

    private Button bt_cancel;
    private Button bt_sure;

    private List<MonitorKindTestBean> tests;

    public MonitorInputPopWindow(Activity context, List<MonitorKindTestBean> tests) {
        this.context = context;
        this.tests = tests;

        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectaddress, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
    }

    private void initViews(View rootView) {
        wheel_addr = (WheelView) rootView.findViewById(R.id.wheel_selectaddress_add);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectaddress_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectaddress_sure);

        String[] ts = new String[tests.size()];
        for (int i = 0; i < tests.size(); i++) {
            ts[i] = tests.get(i).getName();
        }
        ArrayWheelAdapter adapter = new ArrayWheelAdapter(context, ts);
        wheel_addr.setViewAdapter(adapter);
        wheel_addr.setCyclic(false);

        //设置显示行数
        wheel_addr.setVisibleItems(5);

        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    public void show(View parent) {
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectaddress_cancel:

                break;
            case R.id.bt_selectaddress_sure:
                if (listener != null) {
                    listener.select(wheel_addr.getCurrentItem());
                }
                break;
        }
    }

    public interface onTestSelectListener {
        void select(int position);
    }

    private onTestSelectListener listener;

    public void setListener(onTestSelectListener l) {
        listener = l;
    }
}
